home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / g / gnu_c / gpplib22.zoo / libtests / tstring.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-30  |  8.6 KB  |  415 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. /*
  4.  A test file for Strings
  5. */
  6.  
  7. #include <xstring.h>
  8. #include <stream.h>  // see note below on `dec(20)' about why this will go away
  9. #include <std.h>
  10. #include <assert.h>
  11.  
  12. extern "C" long _stksize = 16*1024;
  13.  
  14. // can't nicely echo assertions because they contain quotes
  15.  
  16. #define tassert(ex) {if (!(ex)) \
  17.                      { cerr << "failed assertion at " << __LINE__ << "\n"; \
  18.                        abort(); } }
  19.  
  20.   String X = "Hello";
  21.   String Y = "world";
  22.   String N = "123";
  23.   String c;
  24.   char*  s = ",";
  25.   Regex  r = "e[a-z]*o";
  26.  
  27. void decltest()
  28. {
  29.   String x;
  30.   cout << "an empty String:" << x << "\n";
  31.   assert(x.OK());
  32.   assert(x == "");
  33.  
  34.   String y = "Hello";
  35.   cout << "A string initialized to Hello:" << y << "\n";
  36.   assert(y.OK());
  37.   assert(y == "Hello");
  38.  
  39.   if (y[y.length()-1] == 'o')
  40.     y = y + '\n';
  41.   assert(y == "Hello\n");
  42.   y = "Hello";
  43.  
  44.   String a = y;
  45.   cout << "A string initialized to previous string:" << a << "\n";
  46.   assert(a.OK());
  47.   assert(a == "Hello");
  48.   assert(a == y);
  49.  
  50.   String b (a.at(1, 2));
  51.   cout << "A string initialized to previous string.at(1, 2):" << b << "\n";
  52.   assert(b.OK());
  53.   assert(b == "el");
  54.  
  55.   char ch = '@';
  56.   String z(ch); 
  57.   cout << "A string initialized to @:" << z << "\n";
  58.   assert(z.OK());
  59.   assert(z == "@");
  60.  
  61.   // XXX: `dec' is obsolete.  Since String.h includes iostream.h, and not
  62.   //      stream.h, we include stream.h in this file for the time being.  This
  63.   //      test will be rewritten to be done the "right" way, but for now, let's
  64.   //      save some time and go the easy route.
  65.   String n = dec(20);
  66.   cout << "A string initialized to dec(20):" << n << "\n";
  67.   assert(n.OK());
  68.   assert(n == "20");
  69.  
  70.   int i = atoi(n);
  71.   double f = atof(n);
  72.   cout << "n = " << n << " atoi(n) = " << i << " atof(n) = " << f << "\n";
  73.   assert(i == 20);
  74.   assert(f == 20);
  75.  
  76.   assert(X.OK());
  77.   assert(Y.OK());
  78.   assert(x.OK());
  79.   assert(y.OK());
  80.   assert(z.OK());
  81.   assert(n.OK());
  82.   assert(r.OK());
  83. }
  84.  
  85. void cattest()
  86. {
  87.   String x = X;
  88.   String y = Y;
  89.   String z = x + y;
  90.   cout << "z = x + y = " << z << "\n";
  91.   assert(x.OK());
  92.   assert(y.OK());
  93.   assert(z.OK());
  94.   assert(z == "Helloworld");
  95.  
  96.   x += y;
  97.   cout << "x += y; x = " << x << "\n";
  98.   assert(x.OK());
  99.   assert(y.OK());
  100.   assert(x == "Helloworld");
  101.  
  102.   y = Y;
  103.   x = X;
  104.   y.prepend(x);
  105.   cout << "y.prepend(x); y = " << y << "\n";
  106.   assert(y == "Helloworld");
  107.  
  108.   y = Y;
  109.   x = X;
  110.   cat(x, y, x, x);
  111.   cout << "cat(x, y, x, x); x = " << x << "\n";
  112.   assert(x == "HelloworldHello");
  113.  
  114.   y = Y;
  115.   x = X;
  116.   cat(y, x, x, x);
  117.   cout << "cat(y, x, x, x); x = " << x << "\n";
  118.   assert(x == "worldHelloHello");
  119.  
  120.   x = X;
  121.   y = Y;
  122.   z = x + s + ' ' + y.at("w") + y.after("w") + ".";
  123.   cout << "z = x + s +  + y.at(w) + y.after(w) + . = " << z << "\n";
  124.   assert(z.OK());
  125.   assert(z == "Hello, world.");
  126.  
  127. }
  128.  
  129. void comparetest()
  130. {  
  131.   String x = X;
  132.   String y = Y;
  133.   String n = N;
  134.   String z = x + y;
  135.  
  136.   assert(x != y);
  137.   assert(x == "Hello");
  138.   assert(x != z.at(0, 4));
  139.   assert (x < y);
  140.   assert(!(x >= z.at(0, 6)));
  141.   assert(x.contains("He"));
  142.   assert (z.contains(x));
  143.   assert(x.contains(r));
  144.  
  145.   assert(!(x.matches(r)));
  146.   assert(x.matches(RXalpha));
  147.   assert(!(n.matches(RXalpha)));
  148.   assert(n.matches(RXint));
  149.   assert(n.matches(RXdouble));
  150.  
  151.   assert(x.index("lo") == 3);
  152.   assert(x.index("l", 2) == 2);
  153.   assert(x.index("l", -1) == 3);
  154.   assert(x.index(r)  == 1);
  155.   assert(x.index(r, -2) == 1);
  156.  
  157.   assert(x.contains("el", 1));
  158.   assert(x.contains("el"));
  159.  
  160.   assert(common_prefix(x, "Help") == "Hel");
  161.   assert(common_suffix(x, "to") == "o");
  162.  
  163.   assert(fcompare(x, "hELlo") == 0);
  164.   assert(fcompare(x, "hElp") < 0);
  165. }
  166.  
  167. void substrtest()
  168. {
  169.   String x = X;
  170.  
  171.   char ch = x[0];
  172.   cout << "ch = x[0] = " << ch << "\n";
  173.   assert(ch == 'H');
  174.  
  175.   String z = x.at(2, 3);
  176.   cout << "z = x.at(2, 3) = " << z << "\n";
  177.   assert(z.OK());
  178.   assert(z == "llo");
  179.  
  180.   x.at(2, 2) = "r";
  181.   cout << "x.at(2, 2) = r; x = " << x << "\n";
  182.   assert(x.OK());
  183.   assert(x.at(2,2).OK());
  184.   assert(x == "Hero");
  185.  
  186.   x = X;
  187.   x.at(0, 1) = "j";
  188.   cout << "x.at(0, 1) = j; x = " << x << "\n";
  189.   assert(x.OK());
  190.   assert(x == "jello");
  191.  
  192.   x = X;
  193.   x.at("He") = "je";
  194.   cout << "x.at(He) = je; x = " << x << "\n";
  195.   assert(x.OK());
  196.   assert(x == "jello");
  197.  
  198.   x = X;
  199.   x.at("l", -1) = "i";
  200.   cout << "x.at(l, -1) = i; x = " << x << "\n";
  201.   assert(x.OK());
  202.   assert(x == "Helio");
  203.   
  204.   x = X;
  205.   z = x.at(r);
  206.   cout << "z = x.at(r) = " << z << "\n";
  207.   assert(z.OK());
  208.   assert(z == "ello");
  209.  
  210.   z = x.before("o");
  211.   cout << "z = x.before(o) = " << z << "\n";
  212.   assert(z.OK());
  213.   assert(z == "Hell");
  214.  
  215.   x.before("ll") = "Bri";
  216.   cout << "x.before(ll) = Bri; x = " << x << "\n";
  217.   assert(x.OK());
  218.   assert(x == "Brillo");
  219.  
  220.   x = X;
  221.   z = x.before(2);
  222.   cout << "z = x.before(2) = " << z << "\n";
  223.   assert(z.OK());
  224.   assert(z == "He");
  225.  
  226.   z = x.after("Hel");
  227.   cout << "z = x.after(Hel) = " << z << "\n";
  228.   assert(z.OK());
  229.   assert(z == "lo");
  230.  
  231.   x.after("Hel") = "p";  
  232.   cout << "x.after(Hel) = p; x = " << x << "\n";
  233.   assert(x.OK());
  234.   assert(x == "Help");
  235.  
  236.   x = X;
  237.   z = x.after(3);
  238.   cout << "z = x.after(3) = " << z << "\n";
  239.   assert(z.OK());
  240.   assert(z == "o");
  241.  
  242.   z = "  a bc";
  243.   z  = z.after(RXwhite);
  244.   cout << "z =   a bc; z = z.after(RXwhite); z =" << z << "\n";
  245.   assert(z.OK());
  246.   assert(z == "a bc");
  247. }
  248.  
  249.  
  250. void utiltest()
  251. {
  252.   String x = X;
  253.   int matches = x.gsub("l", "ll");
  254.   cout << "x.gsub(l, ll); x = " << x << "\n";
  255.   assert(x.OK());
  256.   assert(matches == 2);
  257.   assert(x == "Hellllo");
  258.  
  259.   x = X;
  260.   assert(x.OK());
  261.   matches = x.gsub(r, "ello should have been replaced by this string");
  262.   assert(x.OK());
  263.   cout << "x.gsub(r, ...); x = " << x << "\n";
  264.   assert(x.OK());
  265.   assert(matches == 1);
  266.   assert(x == "Hello should have been replaced by this string");
  267.  
  268.   matches = x.gsub(RXwhite, "#");
  269.   cout << "x.gsub(RXwhite, #); x = " << x << "\n";
  270.   assert(matches == 7);
  271.   assert(x.OK());
  272.  
  273.   String z = X + Y;
  274.   z.del("loworl");
  275.   cout << "z = x+y; z.del(loworl); z = " << z << "\n";
  276.   assert(z.OK());
  277.   assert(z == "Held");
  278.  
  279.   x = X;
  280.   z = reverse(x);
  281.   cout << "reverse(x) = " << z << "\n";
  282.   assert(z.OK());
  283.   assert(z == "olleH");
  284.  
  285.   x.reverse();
  286.   cout << "x.reverse() = " << x << "\n";
  287.   assert(x.OK());
  288.   assert(x == z);
  289.  
  290.   x = X;
  291.   z = upcase(x);
  292.   cout << "upcase(x) = " << z << "\n";
  293.   assert(z.OK());
  294.   assert(z == "HELLO");
  295.  
  296.   z = downcase(x);
  297.   cout << "downcase(x) = " << z << "\n";
  298.   assert(z.OK());
  299.   assert(z == "hello");
  300.  
  301.   z = capitalize(x);
  302.   cout << "capitalize(x) = " << z << "\n";
  303.   assert(z.OK());
  304.   assert(z == "Hello");
  305.   
  306.   z = replicate('*', 10);
  307.   cout << "z = replicate(*, 10) = " << z << "\n";
  308.   assert(z.OK());
  309.   assert(z == "**********");
  310.   assert(z.length() == 10);
  311. }
  312.  
  313. void splittest()
  314. {
  315.   String z = "This string\thas\nfive words";
  316.   cout << "z = " << z << "\n";
  317.   String w[10];
  318.   int nw = split(z, w, 10, RXwhite);
  319.   assert(nw == 5);
  320.   cout << "from split(z, RXwhite, w, 10), n words = " << nw << ":\n";
  321.   for (int i = 0; i < nw; ++i)
  322.   {
  323.     assert(w[i].OK());
  324.     cout << w[i] << "\n";
  325.   }
  326.   assert(w[0] == "This");
  327.   assert(w[1] == "string");
  328.   assert(w[2] == "has");
  329.   assert(w[3] == "five");
  330.   assert(w[4] == "words");
  331.   assert(w[5] == (char*)0);
  332.  
  333.   z = join(w, nw, "/");
  334.   cout << "z = join(w, nw, /); z =" << z << "\n";
  335.   assert(z.OK());
  336.   assert(z == "This/string/has/five/words");
  337. }
  338.  
  339.  
  340. void iotest()
  341. {
  342.   String z;
  343.   cout << "enter a word:";
  344.   cin >> z;
  345.   cout << "word =" << z << " ";
  346.   cout << "length = " << z.length() << "\n";
  347. }
  348.  
  349. void identitytest(String a, String b)
  350. {
  351.   String x = a;
  352.   String y = b;
  353.   x += b;
  354.   y.prepend(a);
  355.   assert((a + b) == x);
  356.   assert((a + b) == y);
  357.   assert(x == y);
  358.   assert(x.after(a) == b);
  359.   assert(x.before(b, -1) == a);
  360.   assert(x.from(a) == x);
  361.   assert(x.through(b, -1) == x);
  362.   assert(x.at(a) == a);
  363.   assert(x.at(b) == b);
  364.  
  365.   assert(reverse(x) == reverse(b) + reverse(a));
  366.   
  367.   assert((a + b + a) == (a + (b + a)));
  368.  
  369.   x.del(b, -1);
  370.   assert(x == a);
  371.  
  372.   y.before(b, -1) = b;
  373.   assert(y == (b + b));
  374.   y.at(b) = a;
  375.   assert(y == (a + b));
  376.  
  377.   x = a + reverse(a);
  378.   for (int i = 0; i < 7; ++i)
  379.   {
  380.     y = x;
  381.     x += x;
  382.     assert(x.OK());
  383.     assert(x == reverse(x));
  384.     assert(x.index(y) == 0);
  385.     assert(x.index(y, -1) == x.length() / 2);
  386.   }
  387. }
  388.  
  389. void freqtest()
  390. {
  391.   String x = "Hello World";
  392.   SubString y = x.at(0,5);
  393.   assert(x.freq('l') == 3);    // char
  394.   assert(x.freq("lo") == 1);    // char*
  395.   assert(x.freq(x) == 1);    // String
  396.   assert(x.freq(y) == 1);    // SubString
  397. }
  398.  
  399. int main()
  400. {
  401.   decltest();
  402.   cattest();
  403.   comparetest();
  404.   substrtest();
  405.   utiltest();
  406.   splittest();
  407.   freqtest();
  408.   identitytest(X, X);
  409.   identitytest(X, Y);
  410.   identitytest(X+Y+N+X+Y+N, "A string that will be used in identitytest but is otherwise just another useless string.");
  411.   iotest();
  412.   cout << "\nEnd of test\n";
  413.   return 0;
  414. }
  415.